package com.animee.health.food_grid;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.animee.health.R;
import com.animee.health.bean.FoodBean;
import com.animee.health.utils.DBTools;
import com.animee.health.utils.DataDB;

import java.util.List;

public class FoodGridActivity extends AppCompatActivity {
    GridView gv;        // 网格布局组件
    List<FoodBean> mDatas;
    DataDB db;
    // DataDB db = new DataDB(FoodGridActivity.this, "Data.db", null, 1);
    DBTools tools = new DBTools();
    private FoodGridAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_grid);
        // 查找得到这个控件
        gv = findViewById(R.id.food_grid_gv);
        // 找到数据源,是一个list数据集合(显示全部的内容)
        // mDatas = FoodUtils.getAllFoodList();
        db = new DataDB(FoodGridActivity.this, "Data.db", null, 1);
        mDatas = tools.select(db.getReadableDatabase());
        // 创建适配器对象, 传入mDatas得到mDatas对象
        adapter = new FoodGridAdapter(this, mDatas);
        // 设置适配器, 为ListView指定要显示的列表项
        gv.setAdapter(adapter);
        setListener();
    }


    // ListView点击的单向监听,每点击一mDatas跳转到对应FoodDesc
    private void setListener() {
        // 单向点击监听
        // 需要一个接口,传入当前的对象
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 得到这个对象
                FoodBean foodBean = mDatas.get(position);
                // 通过Intent实现当前页面FoodGridActivity到详情页面的跳转
                Intent intent = new Intent(FoodGridActivity.this, FoodDescActivity.class);
                // 传递虚类化的数据(mplements Serializable:让FoodBean可以实现虚类化的接口)
                intent.putExtra("food", foodBean);
                startActivity(intent);
            }
        });
    }
}
